package com.aqarat.managedBean;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.aqarat.controller.AqaratController;
import com.aqarat.controller.AqaratControllerImpl;
import com.aqarat.entity.Apartment;
import com.aqarat.entity.Building;
import com.aqarat.entity.Floor;
import com.common.Constant;
import com.core.managedBean.BaseBean;
import com.core.util.Utils;

/**
 * @author karimsherif
 * 
 */
@ManagedBean
@ViewScoped
public class BuildingBean extends BaseBean {
	private static final long serialVersionUID = 1L;
	private Building building;
	private AqaratController aqaratController;
	private List<Long> floorList = new ArrayList<Long>();
	private List<Apartment> apartmentList = new ArrayList<Apartment>();
	private List<Building> builgingList;// =new Arra
	private Floor floor;
	private Double allBuildingsExpenses=0.0;
	private Double allBuildingsRevenues=0.0;

	@PostConstruct
	public void loadData() {
		if (aqaratController == null) {
			aqaratController = new AqaratControllerImpl();
		}
		initialize();
		loadMaxFloorsAndApartments();
		loadAllBuildings();
		loadAllEspensesAndRevenus();
	}

	public void initialize() {
		building = new Building();
		floor = new Floor();
	}

	public void loadAllBuildings() {
		try {
			builgingList = aqaratController.getBuildingList(null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void loadAllEspensesAndRevenus() {
		for(Building b:builgingList){
			allBuildingsExpenses=allBuildingsExpenses+b.getExpenses();
			allBuildingsRevenues=allBuildingsRevenues+b.getRevenues();
		}
	}

	public void saveBuilding() {
		try {
			floor.setNumberOfApartments((long) apartmentList.size());
			aqaratController.saveBuilding(building, floor, apartmentList);
			super.addInfoMessage("Successfully Saved");
		} catch (Exception e) {
			e.printStackTrace();
			super.addErrorMessage("An Error Occured when Saving");
		}
	}

	public void loadApartmentList() {
		apartmentList.clear();
		for (Long l = 0L; l < building.getNumberOfFloors(); l++) {
			apartmentList.add(new Apartment());
		}
	}

	public void loadMaxFloorsAndApartments() {
		for (Long m = 1L; m <= Constant.MAX_FLOORS; m++) {
			floorList.add(m);
		}
	}

	public void addApartment() {
		Apartment a = new Apartment();
		a.setNo((long) apartmentList.size() + 1);
		apartmentList.add(a);
	}

	public void deleteApartment(Apartment apartment) {
		if (Utils.isEmpty(apartment.getRecid())) {
			apartmentList.remove(apartment);
		} else {
			try {
				aqaratController.deleteApartment(apartment);
				apartmentList.remove(apartment);
				super.addInfoMessage("Successfully Deleted");

			} catch (Exception e) {
				super.addErrorMessage("An Error occured");
				e.printStackTrace();
			}
		}
	}

	// ------------------------------------//
	public void setBuilding(Building building) {
		this.building = building;
	}

	public Building getBuilding() {
		return building;
	}

	public final List<Long> getFloorList() {
		return floorList;
	}

	public final void setFloorList(List<Long> floorList) {
		this.floorList = floorList;
	}

	public final List<Apartment> getApartmentList() {
		return apartmentList;
	}

	public final void setApartmentList(List<Apartment> apartmentList) {
		this.apartmentList = apartmentList;
	}

	public final Floor getFloor() {
		return floor;
	}

	public final void setFloor(Floor floor) {
		this.floor = floor;
	}

	public final List<Building> getBuilgingList() {
		return builgingList;
	}

	public final void setBuilgingList(List<Building> builgingList) {
		this.builgingList = builgingList;
	}

	public final Double getAllBuildingsExpenses() {
		return allBuildingsExpenses;
	}

	public final void setAllBuildingsExpenses(Double allBuildingsExpenses) {
		this.allBuildingsExpenses = allBuildingsExpenses;
	}

	public final Double getAllBuildingsRevenues() {
		return allBuildingsRevenues;
	}

	public final void setAllBuildingsRevenues(Double allBuildingsRevenues) {
		this.allBuildingsRevenues = allBuildingsRevenues;
	}

}
